package com.beijiteshop.libcommon.handler

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import java.lang.ref.WeakReference


/**
 * @Author LiaoZhongKai
 * @Date 2021/3/4 17:18
 * @Description
 */
abstract class WeakHandler private constructor(): Handler(Looper.getMainLooper()) {

    private lateinit var mWeakReference: WeakReference<Context>

    constructor(context: Context) : this() {
        mWeakReference = WeakReference(context)
    }

    override fun handleMessage(msg: Message) {
        val context = mWeakReference.get()
        context?.let {
            handleMsg(msg)
        }
    }

    /**
     * 抽象方法用户实现,用来处理具体的业务逻辑
     *
     * @param msg
     * @param what [What]
     */
    abstract fun handleMsg(msg: Message)
}